package com.qf.service.impl;

import com.qf.api.CacheFeign;
import com.qf.constant.CacheConstants;
import com.qf.constant.StrategyConstants;
import com.qf.model.StandardSubmit;
import com.qf.service.FilterChainService;
import com.qf.util.PushMsgUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author WuZiYu
 * @date 2022-6-8 下午 7:31
 * 扣费策略
 */
@Service("fee")
@Slf4j
public class FeeFilterServiceImpl implements FilterChainService {

    @Autowired
    private PushMsgUtil pushMsgUtil;
    @Autowired
    private CacheFeign cacheFeign;

    /**
     * 扣费策略
     * @param standardSubmit
     * @return
     */
    @Override
    public boolean strategy(StandardSubmit standardSubmit) {
        log.info("[策略模块 - 扣费策略] 策略开始执行");
        String key= CacheConstants.CACHE_PREFIX_CLIENT+standardSubmit.getClientID();
        //1客户剩余总金额
        Object fee = cacheFeign.hget(key, "fee");
        //2本次手机短信费用
//        String price = (String) cacheFeign.hget(CacheConstants.CACHE_PREFIX_ROUTER
//                + standardSubmit.getClientID() + ":" + standardSubmit.getGatewayID(), "price");
        Long price = standardSubmit.getFee();
        boolean flag=true;
        if (fee instanceof Long){//防止类型转换异常
            Long f=(Long) fee;
            flag=comparePrice(f,price);
        }else if (fee instanceof Integer){
            Integer f=(Integer)fee;
            flag=comparePrice(f,price);
        }else if (fee instanceof String){
            String f=(String)fee;
            flag=comparePrice(f,price);
        }

        if (flag){
            //客户信息中  fee  1066000
            //redis  incr  fee -40
            Long surplusFee = cacheFeign.hincr(key, "fee", -price);
            //判断客户的总金额是否小于0  防止多线程高并发的情况下  后面的线程可能判断金额是有问题
            //扣费之后的剩余费用  是否小于0
            if (surplusFee<0){
                //把费用加回去
                log.info("[策略模块 - 扣费策略] 扣费策略执行失败。。。用户剩余金额不足。。。");
                cacheFeign.hincr(key, "fee", price);
                //打印日志  发送到MQ
                pushMsgUtil.sendLog(standardSubmit, StrategyConstants.STRATEGY_ERROR_FEE);
                pushMsgUtil.pushReport(standardSubmit, StrategyConstants.STRATEGY_ERROR_FEE);
                return false;
            }
            //成功
            log.info("[策略模块 - 扣费策略] 扣费策略执行成功！！！");
            return true;
        }else {
            log.info("[策略模块 - 扣费策略] 扣费策略执行失败。。。用户剩余金额不足。。。");
            //打印日志  发送到MQ
            pushMsgUtil.sendLog(standardSubmit, StrategyConstants.STRATEGY_ERROR_FEE);
            pushMsgUtil.pushReport(standardSubmit, StrategyConstants.STRATEGY_ERROR_FEE);
            return false;
        }
    }

    //比较客户剩余总金额  本次手机短信费用
    public boolean comparePrice(Long total,Long p){
        System.out.println("===Long===");
        if (total>=p){
            return true;//扣费成功
        }else {
            return false;//扣费失败
        }
    }
    //比较客户剩余总金额  本次手机短信费用
    public boolean comparePrice(Integer total,Long p){
        System.out.println("===Integer===");
        if (total>=p){
            return true;//扣费成功
        }else {
            return false;//扣费失败
        }
    }
    //比较客户剩余总金额  本次手机短信费用
    public boolean comparePrice(String f,Long p){
        System.out.println("===String===");
        int total = Integer.parseInt(f);
        if (total >= p){
            return true;//扣费成功
        }else {
            return false;//扣费失败
        }
    }
}
